package dxunderground.winampController.Client.GUI;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;

import dxunderground.winampController.Common.ButtonActionsInterface;
import dxunderground.winampController.Common.GUI;
import dxunderground.winampController.Client.Playlist;

public class ButtonActions implements ButtonActionsInterface {
	private GUI gui;
	private Playlist playlist;

	public ButtonActions(GUI gui, Playlist playlist) {
		this.gui = gui;
		this.playlist = playlist;
	}

	public void filter() {
		ArrayList<File> songs = this.playlist.getSongs();
		gui.listModel.clear();
		for (int i = 0; i < songs.size(); i++) {
			File file = songs.get(i);
			if (file.getAbsolutePath().toUpperCase().indexOf(gui.filterField.getText().toUpperCase()) >= 0) {
				gui.listModel.addElement(file);
			}
		}
	}

	public void add() {
		int[] songs = gui.list.getSelectedIndices();
		for (int i = 0; i < songs.length; i++) {
			playlist.addSong(gui.listModel.get(songs[i]).toString());
		}
	}

	public void previous() {
		playlist.prevSong("<<");
	}

	public void playPause() {
		playlist.prevSong("||>");
	}

	public void stop() {
		playlist.prevSong("[]");
	}

	public void next() {
		playlist.prevSong(">>");
	}

	@Override
	public void actionPerformed(ActionEvent command) {
		if (command.getActionCommand().equals("Filter")) {
			this.filter();
		} else if (command.getActionCommand().equals("Add to Playlist")) {
			this.add();
		} else if (command.getActionCommand().equals("<<")) {
			this.previous();
		} else if (command.getActionCommand().equals("||>")) {
			this.playPause();
		} else if (command.getActionCommand().equals("[]")) {
			this.stop();
		} else if (command.getActionCommand().equals(">>")) {
			this.next();
		} else {
			System.err.println(command.getActionCommand());
		}
	}
}
